package academic.concordia.bankofquebec.bean.client;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;

import academic.concordia.bankofquebec.bean.authentication.MyAuthenticationToken;
import academic.concordia.bankofquebec.bean.session.SessionData;
import academic.concordia.bankofquebec.dao.client.IClientDAO;
import academic.concordia.bankofquebec.dao.identificationtype.IIdentificationTypeDAO;
import academic.concordia.bankofquebec.entity.BankAccount;
import academic.concordia.bankofquebec.entity.Bill;
import academic.concordia.bankofquebec.entity.Client;
import academic.concordia.bankofquebec.entity.IdentificationType;

@Transactional(readOnly = true)
public class ClientBean implements IClientBean {
	
	IClientDAO clientDAO;

	@Transactional(readOnly = false)
	@Override
	public String createClient(Client client) {
		// TODO Manage the client creation in DB and return an error message if something fails
		try {
			clientDAO.addClient(client);
			return "SUCCESS";
		} catch(Exception e) {
			System.out.println("ERROR CREATING CLIENT: " + client.getName());
			return "ERROR";
		}
	}

	@Override
	public List<IdentificationType> getIdentificationTypes() {
		return new ArrayList<IdentificationType>(Arrays.asList(IdentificationType.values()));
	}

	@Override
	public Client authenticateClient(Client client) {
		return clientDAO.validateClientAccess(client);
	}

	public IClientDAO getClientDAO() {
		return clientDAO;
	}

	public void setClientDAO(IClientDAO clientDAO) {
		this.clientDAO = clientDAO;
	}

	@Override
	public List<BankAccount> getClientProducts(Client client) {
		return clientDAO.getClientById(client.getIdNumber()).getProducts();
	}

	@Override
	public List<Bill> getBills(Client client) {
		System.out.println("client.getIdNumber(): " + client.getIdNumber());
		return clientDAO.getClientById(client.getIdNumber()).getBills();
	}
	
	
}
